package exactSearch;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
public class SectionSearch {


	static PrintStream ps;

	public static void main(String[] args) throws IOException {

		ArrayList<String> al=list();
		FileReader ff = new FileReader(
				"J:\\BC3_IMT_Training\\annotations\\IMT_training_pmids.txt");

		BufferedReader fb = new BufferedReader(ff);
		String name = fb.readLine();

		ArrayList<String> a = new ArrayList<String>();
		ps = new PrintStream("J:\\BC3_IMT_Training\\Sections Found.txt");
		while (name != null) {

			String s="";
				FileReader fr = new FileReader(
						"J:\\BC3_IMT_Training\\Sections\\" + name + "\\"+name+".txt");
				BufferedReader fbr = new BufferedReader(fr);
				int count=0;
				String str = fbr.readLine();
				while (str != null) {	
					FileReader dfr = new FileReader("J:\\BC3_IMT_Training\\PSIMI.txt");
					BufferedReader dfbr = new BufferedReader(dfr);
					String f = dfbr.readLine();
					while (f != null) {
						String dict = f.substring(9);
						String did= f.substring(0, 7);
						
					count++;
					if(al.contains(str)){
						s= str;
					}	
						int ind=str.indexOf(dict); 

						if (ind!= -1) {
							String st = name+"\t"+did+"\t"+ind;
							
							if(a.contains(st)==false){
								ps.println(name+"\t"+s+"\t"+f+"\t"+count+"\t"+ind);
								a.add(st);
							}
							


						}
					
						f = dfbr.readLine();
				}
					str = fbr.readLine();
				

			}
			name=fb.readLine();
		}
	}


	public static ArrayList<String> list() throws IOException{

		ArrayList<String> al = new ArrayList<String>();
		FileReader fr = new FileReader("J:\\BC3_IMT_Training\\Research Paper Terms.txt");
		BufferedReader fbr = new BufferedReader(fr);
		String str= fbr.readLine();
		while(str!=null){
			al.add(str);
			str=fbr.readLine();
		}
		return al;

	}

}
